package com.koo.modules.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.utils.MyStrUtil;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.modules.product.dao.ProductSupplierDao;
import com.koo.modules.product.entity.ProductSupplierEntity;
import com.koo.modules.product.entity.vo.ImageVo;
import com.koo.modules.product.service.ProductSupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ProductSupplierServiceImpl extends ServiceImpl<ProductSupplierDao, ProductSupplierEntity> implements ProductSupplierService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<ProductSupplierEntity> queryWrapper = new LambdaQueryWrapper<>();
        Object dataForm = params.get("dataForm");
        if (MyStrUtil.isNotEmpty(dataForm)) {
            JSONObject jsonObject = JSON.parseObject(dataForm.toString());
            String key = jsonObject.getString("key");
            queryWrapper.like(MyStrUtil.isNotEmpty(key), ProductSupplierEntity::getSupplierName, key)
                    .or().like(MyStrUtil.isNotEmpty(key), ProductSupplierEntity::getAddress, key);
        }
        IPage<ProductSupplierEntity> page = this.page(
                new Query<ProductSupplierEntity>().getPage(params),
                queryWrapper
        );

        page.getRecords().forEach(item -> {
            List<ImageVo> list = new ArrayList<>();
            if (MyStrUtil.isNotEmpty(item.getBusinessLicense())) {
                String[] pics = item.getBusinessLicense().split(",");
                for (String url : pics) {
                    ImageVo imageVo = new ImageVo();
                    imageVo.setUrl(url);
                    list.add(imageVo);
                }
            }
            item.setLicensePics(list);

            List<ImageVo> businessList = new ArrayList<>();
            if (MyStrUtil.isNotEmpty(item.getProductBusinessLlicense())) {
                String[] pics = item.getProductBusinessLlicense().split(",");
                for (String url : pics) {
                    ImageVo imageVo = new ImageVo();
                    imageVo.setUrl(url);
                    businessList.add(imageVo);
                }
            }
            item.setBusinessPics(businessList);
        });
        return new PageUtils(page);
    }

    @Override
    public List<ProductSupplierEntity> list(Long storeId) {
        LambdaQueryWrapper<ProductSupplierEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyStrUtil.isNotEmpty(storeId), ProductSupplierEntity::getStoreId, storeId);
        return this.list(wrapper);
    }


    @Override
    public List<ProductSupplierEntity> getSupplies(Long storeId) {
        LambdaQueryWrapper<ProductSupplierEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyStrUtil.isNotEmpty(storeId), ProductSupplierEntity::getStoreId, storeId);
        return this.list(wrapper);
    }
}